package com.qingfeng.medical.controller.admin.hosp;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.qingfeng.medical.base.controller.BaseController;
import com.qingfeng.medical.base.log.annotation.Log;
import com.qingfeng.medical.base.log.enums.BusinessType;
import com.qingfeng.medical.base.result.Result;
import com.qingfeng.medical.biz.hosp.domain.dto.HospDepartmentSaveDTO;
import com.qingfeng.medical.biz.hosp.domain.dto.HospDepartmentUpdateDTO;
import com.qingfeng.medical.biz.hosp.domain.dto.HospDepartmentUpdateStatusDTO;
import com.qingfeng.medical.biz.hosp.domain.entity.HospDepartment;
import com.qingfeng.medical.biz.hosp.domain.vo.DepPatientVo;
import com.qingfeng.medical.biz.hosp.domain.vo.HospDepartmentVo;
import com.qingfeng.medical.biz.hosp.service.HospDepartmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotBlank;
import java.util.List;
import java.util.stream.Collectors;


/**
 * 科室管理
 *
 * @author 清风学Java
 * @date 2023-04-08 23:43:31
 */
@Api(tags = "科室管理管理")
@RestController
@RequestMapping("/admin/hosp/hospDepartment")
@Validated
public class HospDepartmentController extends BaseController {

    @Autowired
    private HospDepartmentService hospDepartmentService;

    @PreAuthorize("hasAuthority('bnt.hospdep.list')")
    @ApiOperation(value = "科室-门诊管理列表", notes = "科室-门诊管理列表")
    @GetMapping("/list")
    @Log(title = "科室管理", businessType = BusinessType.SELECT, message = "科室-门诊管理列表")
    public Result<List<HospDepartmentVo>> list(@ApiParam(name = "depName", value = "科室名", required = false)
                                               @RequestParam("depName") String depName,
                                               @ApiParam(name = "patientName", value = "门诊名", required = false)
                                               @RequestParam("patientName") String patientName) {
        List<HospDepartmentVo> hospDepartmentVoList = hospDepartmentService.hospDepartmentList(depName, patientName);
        return Result.ok(hospDepartmentVoList);
    }

    @PreAuthorize("hasAuthority('bnt.hospdep.add')")
    @ApiOperation(value = "保存科室管理信息", notes = "保存科室管理信息")
    @PostMapping("/save")
    @Log(title = "科室管理", businessType = BusinessType.INSERT, message = "保存科室管理信息")
    public Result saveHospDepartment(@RequestBody @Validated HospDepartmentSaveDTO hospDepartmentSaveDTO) {
        hospDepartmentService.saveHospDepartment(hospDepartmentSaveDTO);
        return Result.ok();
    }

    @PreAuthorize("hasAuthority('bnt.hospdep.update')")
    @ApiOperation(value = "修改科室-门诊信息", notes = "修改科室门诊信息")
    @PutMapping("/update")
    @Log(title = "科室管理", businessType = BusinessType.UPDATE, message = "修改科室门诊信息")
    public Result update(@RequestBody @Validated HospDepartmentUpdateDTO hospDepartmentUpdateDTO) {
        hospDepartmentService.updateHospDepartment(hospDepartmentUpdateDTO);
        return Result.ok();
    }

    @PreAuthorize("hasAuthority('bnt.hospdep.delete')")
    @ApiOperation(value = "根据科室删除信息", notes = "根据科室删除信息")
    @DeleteMapping("/delete/dep")
    @Log(title = "科室管理", businessType = BusinessType.DELETE, message = "根据科室删除信息")
    public Result deleteByDep(@RequestParam("depName") @NotBlank(message = "科室名不能为空") String depName) {
        // TODO 删除的时候检测正在预约中的用户

        hospDepartmentService.remove(
                new LambdaQueryWrapper<HospDepartment>()
                        .eq(HospDepartment::getDepName, depName)
        );
        return Result.ok();
    }

    @PreAuthorize("hasAuthority('bnt.hospdep.delete')")
    @ApiOperation(value = "删除科室门诊信息", notes = "删除科室门诊信息")
    @DeleteMapping("/delete")
    @Log(title = "科室管理", businessType = BusinessType.DELETE, message = "删除科室门诊信息")
    public Result delete(@RequestParam("id") Long id) {
        // TODO 删除的时候检测正在预约中的用户

        hospDepartmentService.removeById(id);
        return Result.ok();
    }

    @ApiOperation(value = "查询全部科室信息", notes = "查询全部科室信息")
    @GetMapping("/findAll/dep")
    @Log(title = "科室管理", businessType = BusinessType.SELECT, message = "查询全部科室信息")
    public Result<List<String>> findAllDeploymentsTypes() {
        return Result.ok(
                hospDepartmentService.list()
                        .stream()
                        .map(HospDepartment::getDepName)
                        .distinct()
                        .collect(Collectors.toList())
        );
    }

    @ApiOperation(value = "根据科室名查询门诊信息", notes = "根据科室名查询门诊信息")
    @GetMapping("/find/patientName")
    @Log(title = "科室管理", businessType = BusinessType.SELECT, message = "根据科室名查询门诊信息")
    public Result<List<String>> findPatientNameByDepName(
            @ApiParam(name = "depName", value = "科室名", required = true)
            @RequestParam("depName") @NotBlank String depName) {
        return Result.ok(
                hospDepartmentService.list(
                                new LambdaQueryWrapper<HospDepartment>()
                                        .eq(HospDepartment::getDepName, depName)
                        ).stream()
                        .map(HospDepartment::getPatientName)
                        .collect(Collectors.toList())
        );
    }

    @PreAuthorize("hasAuthority('bnt.hospdep.update')")
    @ApiOperation(value = "修改科室门诊状态", notes = "修改科室门诊状态")
    @PutMapping("/update/status")
    @Log(title = "获取科室门诊信息", businessType = BusinessType.UPDATE, message = "修改科室门诊状态")
    public Result updateStatus(@RequestBody HospDepartmentUpdateStatusDTO hospDepartmentUpdateStatusDTO){
        // TODO 修改状态的时候，要检查是否有预约的用户

        hospDepartmentService.updateStatus(hospDepartmentUpdateStatusDTO);
        return Result.ok();
    }

    @PreAuthorize("hasAuthority('bnt.hopsdep.patient')")
    @ApiOperation(value = "获取科室门诊信息", notes = "获取科室门诊信息")
    @GetMapping("/tree/dep/patient")
    @Log(title = "获取科室门诊信息", businessType = BusinessType.SELECT, message = "获取科室门诊信息")
    public Result<List<DepPatientVo>> findDepPatient(){
        List<DepPatientVo> depPatientVoList = hospDepartmentService.findDepPatient();
        return Result.ok(depPatientVoList);
    }

    @ApiOperation(value = "导出科室模板", notes = "导出科室模板")
    @GetMapping("/excel/export_template")
    @Log(title = "科室管理", businessType = BusinessType.EXPORT, message = "导出科室模板")
    public void exportTemplate(HttpServletResponse response) {
        hospDepartmentService.exportTemplate(response);
    }

    @ApiOperation(value = "导出科室-门诊信息", notes = "导出科室-门诊信息")
    @GetMapping("/excel/export_department")
    @Log(title = "科室管理", businessType = BusinessType.EXPORT, message = "导出科室-门诊信息")
    public void exportDict(HttpServletResponse response) {
        hospDepartmentService.exportDict(response);
    }

    @ApiOperation(value = "科室门诊信息Excel导入", notes = "科室门诊信息Excel导入")
    @PostMapping("/excel/import/department")
    @Log(title = "科室门诊管理", businessType = BusinessType.IMPORT, message = "科室门诊信息Excel导入")
    public Result departmentImport(MultipartFile file) {
        hospDepartmentService.importDepartment(file);
        return Result.ok();
    }

}
